package com.xxkj.drugtraceability.ui.departdrug

import android.text.Html
import android.view.LayoutInflater
import android.view.View
import androidx.databinding.DataBindingUtil
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.hjq.shape.layout.ShapeLinearLayout
import com.xxkj.drugtraceability.R
import com.xxkj.drugtraceability.bean.DepartDrugInfo
import com.xxkj.drugtraceability.bean.DrugInfoBean
import com.xxkj.drugtraceability.databinding.LayoutDrugScan1Binding
import com.xxkj.drugtraceability.databinding.LayoutPrescriptiondrugcollectionBinding
import com.xxkj.drugtraceability.ext.clickN

/**
 * author: zhoujingjin
 * created on: 2024/9/10 16:52
 * description:
 */
class DepartDrugCollectionAdapter(private val type:Int,private val actionClick:(index:Int)->Unit,private val deleteAction:(drugInfo:DepartDrugInfo)->Unit):BaseQuickAdapter<DepartDrugInfo, BaseDataBindingHolder<LayoutPrescriptiondrugcollectionBinding>>(R.layout.layout_prescriptiondrugcollection) {
    private var scan = false
    fun setCanScan(scan:Boolean){
        this.scan = scan
    }
    override fun convert(
        holder: BaseDataBindingHolder<LayoutPrescriptiondrugcollectionBinding>,
        item: DepartDrugInfo
    ) {
        holder.dataBinding?.apply {
//            val buffer = StringBuffer()
//            buffer.append("<font color='#33333F' size='50px'>")
//            buffer.append("<b>"+item.mc+"    "+"</b>")
//            buffer.append("</font>")
//            buffer.append("<font color='#494953' size='24px'>")
//            buffer.append(item.dj+"*"+item.fysl+"/"+item.gg)
//            buffer.append("</font>")
            tvContent.text = item.mc
            tvGg.text = item.gg+"/"+item.dwmc
            if (item.ygh.isNullOrEmpty()){
                tvHjh.text = "货架号：无"
            }else{
                tvHjh.text = "货架号："+item.ygh
            }
            tvRight.text = item.fysl?.toDouble()?.toInt()?.toString()+(item.dwmc?:"")
            llAdd.removeAllViews()
                //被选中
            if (type==0){
                llParent.isSelected = item.isSelected
                llParent.setOnClickListener {
                    item.isSelected = !item.isSelected
                    if (item.isSelected){
                        actionClick.invoke(getItemPosition(item))
                    }else{
                        actionClick.invoke(-1)
                    }
                    notifyDataSetChanged()
                }
            }
            if (item.drugTracInfos==null){
                item.drugTracInfos = mutableListOf()
            }
            if (item.drugTracInfos.isNullOrEmpty()){
                llBottom.visibility = View.GONE
            }else{
                llBottom.visibility = View.VISIBLE
                item.drugTracInfos?.forEachIndexed { index, drugTrace ->
                    if (index==0||item.showMore){
                        val viewDrug =  LayoutInflater.from(context).inflate(R.layout.layout_drug_scan_1,null)
                        val bind = DataBindingUtil.bind<LayoutDrugScan1Binding>(viewDrug)
                        bind?.apply {
                            data =drugTrace.drugTracCodg
                            if (!item.showMore){
                                if (item.drugTracInfos!!.size>1){
                                    llMore.visibility = View.VISIBLE
                                    tvMore.text = "更多"
                                    ivDown.setImageResource(R.mipmap.icon_zhankai)
                                    llMore.clickN {
                                        item.showMore = true
                                        notifyItemChanged(getItemPosition(item))
                                    }
                                }else{
                                    llMore.visibility = View.GONE
                                }
                            }else{
                                if (item.drugTracInfos!!.size>1&&index==item.drugTracInfos!!.size-1){
                                    //最后一个收起
                                    llMore.visibility = View.VISIBLE
                                    tvMore.text = "收起"
                                    ivDown.setImageResource(R.mipmap.icon_shouqi)
                                    llMore.clickN {
                                        item.showMore = false
                                        notifyItemChanged(getItemPosition(item))
                                    }
                                }else{
                                    llMore.visibility = View.GONE
                                }
                            }
                            if (scan){
                                ivDelete.visibility = View.VISIBLE
                                ivDelete.tag = viewDrug
                                ivDelete.clickN {
                                    //删除
                                    item.drugTracInfos?.remove(drugTrace)
                                    llAdd.removeView(ivDelete.tag as View)
                                    notifyItemChanged(getItemPosition(item))
                                    if (item.drugTracInfos.isNullOrEmpty()&&type==1){
                                        //清除完了，需要回到未扫页面
                                        deleteAction.invoke(item)
                                    }
                                }
                            }else{
                                ivDelete.visibility = View.INVISIBLE
                            }

                            llAdd.addView(viewDrug)
                        }
                    }

                }

            }
        }

        }
}